package org.sanguine.study.hometasks.second.auxiliary;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Word {
	public static final Pattern WORD_PATTERN = Pattern.compile("[a-zA-Z']+[\\-\\_]?[a-zA-Z']+");
	
	private String word;
	private boolean caseSensitivity;
	
	public Word(String text, boolean caseSensitivity) {
		if (text == null) throw new IllegalArgumentException("Word string must not be null");
		word = text.trim();
		
		Matcher matcher = WORD_PATTERN.matcher(word);
		if (!matcher.matches()) throw new IllegalArgumentException("Word string does not match word pattern");
		this.caseSensitivity = caseSensitivity;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (caseSensitivity ? 1231 : 1237);
		result = prime * result + ((word == null) ? 0 : (caseSensitivity) ? word.hashCode() : word.toLowerCase().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (!(obj instanceof Word)) return false;
		
		Word other = (Word) obj;
		if (caseSensitivity != other.caseSensitivity) return false;
		if (word == null) {
			if (other.word != null) return false;
			else return true;
		} else
			if (caseSensitivity) return word.equals(other.word);
			else return word.equalsIgnoreCase(other.word);
	}

	@Override
	public String toString() {
		return word;
	}
}
